/**
 * the Share class represents a share on the stock exchange
 */

package stocks;

public class Share {
    private String stockName;
    private int onStockAvailable;
    private float exchangeRate;

    /**
     * constructor to create a new share
     * @param _stockName the name of the share
     * @param _exchangeRate the exchange rate
     * @param amountOnStock the total amount of shares on the stock
     */
    public Share (String _stockName, float _exchangeRate, int amountOnStock) {
        stockName = _stockName;
        exchangeRate = _exchangeRate;
        onStockAvailable = amountOnStock;
    }
    /**
     * this will return how many shares are still available on the market
     * @return the amount of shares available on the market
     */
    public int getSharesAmount () {
        return onStockAvailable;
    }
    /**
     * this will return the name of the stock
     * @return the name of the stock
     */
    public String getStockName () {
        return stockName;
    }
    /**
     * this will return the current exchange rate
     * @return the current exchange rate
     */
    public float getExchangeRate () {
        return exchangeRate;
    }
    /**
     * buy a certain amount of the share, will lower the availability and higher
     * the demand, thus increasing the price
     * @param amount the amount of shares to be bought
     * @return amount the amount of shares that were really bought, e.g. if
     * someone wants to buy 10 shares and there are only 3 shares available then
     * 3 shares will returned
     */
    public int buy (int amount) {
        if (exchangeRate > 0) {
            if (onStockAvailable >= amount) {
                onStockAvailable -= amount;
                exchangeRate += amount*0.1f;
                return amount;
            }
            else {
                int ret = onStockAvailable;
                onStockAvailable = 0;
                exchangeRate += ret*0.1f;
                return ret;
            }
        }
        else {
            //share is worth nothing, don't sell any more of these shares
            return 0;
        }
    }
    /**
     * sell a certain amount of the share, will higher the availability and lower
     * the demand, thus decreasing the price
     * @param amount the amount of shares to be sold
     */
    public void sell (int amount) {
        onStockAvailable += amount;
        exchangeRate -= amount*0.1f;
        if (exchangeRate <= 0) {
            exchangeRate = 0;
        }
    }
}

